package com.aaa.service.sas.impl;

import com.aaa.dao.sas.TSasSubMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.sas.Page;
import com.aaa.entity.sas.TSasSub;
import com.aaa.service.sas.TSasSubService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class TSasSubServiceImpl  implements TSasSubService {
@Autowired
private TSasSubMapper tSasSubMapper;
    @Override
    public void dealFile(MultipartFile file) {
        int i=tSasSubMapper.selectCount(null);
        if (i>0){
            return;
        }else {
            InputStream is = null;
            try {
                is=file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            EasyExcel.read(is, TSasSub.class, new AnalysisEventListener<TSasSub>() {


                public  static  final  int  BATCH_COUNT =100;
                
                private  List<TSasSub> cachedDataList =new ArrayList<>();

                @Override
                public void invoke(TSasSub data, AnalysisContext context) {
                    cachedDataList.add(data);
                    if (cachedDataList.size()>=BATCH_COUNT){
                        saveData();
                        cachedDataList=new ArrayList<>();
                    }

                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                  saveData();
                }
                private void saveData() {
                    save(cachedDataList);
                }

            }).sheet("焦虑自评量表").doRead();
        }
     }

    @Override
    public void save(List<TSasSub> list) {
        tSasSubMapper.insertSasSubjects(list);

    }

    @Override
    public PageInfo getAll(MyPage myPage) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        return new PageInfo(tSasSubMapper.selectAll());
    }

    @Override
    public boolean updEdit(TSasSub tSasSub) {
        int i=-1;
        if (tSasSub.getId()==null){
            i=tSasSubMapper.insert(tSasSub);
        }else {
          i=tSasSubMapper.updateByPrimaryKeySelective(tSasSub);
        }

        return i>0?true:false;
    }

    @Override
    public boolean del(TSasSub tSasSub) {
        int delete=tSasSubMapper.delete(tSasSub);
        return delete>0?true:false;
    }

    @Override

    public PageInfo queryAll(Page page, TSasSub tSasSub) {
        PageHelper.startPage(page.getPage(),page.getLimit());
        List<TSasSub> tSasSubs = tSasSubMapper.queryAll(tSasSub);
        return new PageInfo(tSasSubs);
    }
}
